package com.pet.controller;

import com.pet.pojo.Result;
import com.pet.pojo.SysUser;
import com.pet.service.ISysUserService;
import com.pet.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class SysUserController {
    // controller层调用service
    @Autowired
    private ISysUserService userService;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result sendCode(String email) {
        SysUser user = userService.findByEmail(email);

        // For registration, we want to check if email exists
        if (user != null && email.contains("register"))
            return new Result("404", "该邮箱已被注册!");
        else if (user == null && !email.contains("register"))
            return new Result("404", "该邮箱未注册!");

        // 生成6位随机验证码
        int n = (int) (Math.random() * 900000 + 100000);
        String serverCode = String.valueOf(n);

        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom("3457714405@qq.com");
        msg.setSubject("爱宠之家验证码");
        msg.setText("您的验证码是: " + serverCode + "\n验证码40分钟内有效。");
        msg.setTo(email);

        mailSender.send(msg);

        // 存储验证码到redis
        redisTemplate.opsForValue().set(email, serverCode, 40, TimeUnit.MINUTES);

        return new Result("200", "发送验证码成功", serverCode);
    }

    @PostMapping
    public Result login(String email, String code) {
        SysUser user = userService.findByEmail(email);
        if (user == null)
            return new Result("404", "邮箱不存在!");

        String serverCode = (String) redisTemplate.opsForValue().get(email);

        if (serverCode != null && serverCode.equals(code)) {
            String token = JWTUtils.geneJsonWebToken(user);
            return new Result("200", "登录成功!", token);
        }

        return new Result("500", "登录失败!验证码不正确或已过期!");
    }

    @PostMapping("/register")
    public Result register(@RequestBody SysUser user) {
        // 验证验证码
        String serverCode = (String) redisTemplate.opsForValue().get(user.getEmail() + "register");
        if (serverCode == null || !serverCode.equals(user.getPassword())) {
            return new Result("500", "验证码不正确或已过期!");
        }

        // 检查邮箱是否已注册
        SysUser existUser = userService.findByEmail(user.getEmail());
        if (existUser != null) {
            return new Result("500", "该邮箱已被注册!");
        }

        // 设置默认角色和头像
        user.setRole(0); // 0表示普通用户
        user.setHeaderImg("/images/default-avatar.png");

        // 保存用户
        int result = userService.insertUser(user);
        if (result > 0) {
            // 生成token
            String token = JWTUtils.geneJsonWebToken(user);
            return new Result("200", "注册成功!", token);
        }

        return new Result("500", "注册失败,请稍后重试!");
    }
}
